#include "bigenemy.h"

BigEnemy::BigEnemy(QGraphicsScene *m_scene)
{
    root = QFileInfo(__FILE__).absolutePath();
    scene = m_scene;
    isshowblood = 0;
    blood = 5000;
    current_blood = 5000;
    current_blood_front_x = 79;
    blood_front_x = 79;




    blood_gb = new QPixmap(root + "/res/doom_slider_back.png");
    *blood_gb = blood_gb->scaled(81,14);
    blood_front = new QPixmap(root + "/res/doom_slider_front.png");
    *blood_front = blood_front->scaled(current_blood_front_x,10);

    blood_gb = new QPixmap(root + "/res/doom_slider_back.png");
    *blood_gb = blood_gb->scaled(81,14);
    blood_front = new QPixmap(root + "/res/doom_slider_front.png");
    *blood_front = blood_front->scaled(current_blood_front_x,10);


    setVisible(false);
    speed = 1.0f;
    Rotate = 0.0f;
    StartRotate = 0.0f;
    for(int i=0;i<6;i++){
        pix.append(QPixmap(root+"/res/big_bad_frame"+QString::number(i+1)+".png"));
    }

    //pix.append(QPixmap(root+"/res/big_bad_frame1.png"));
    state = 0;
    stopTime = 0;
    scene->addItem(this);
}

QRectF BigEnemy::boundingRect() const
{
    return QRectF(-pix[0].width()*0.5,-pix[0].height()*0.5,pix[0].width(),pix[0].height()-10);
}

void BigEnemy::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(isshowblood == 1){
        painter->drawPixmap(-blood_gb->width()*0.5,-pix[0].height()*0.5-10,*blood_gb);
        painter->drawPixmap(-blood_gb->width()*0.5+1,-pix[0].height()*0.5-9,blood_front->scaled(current_blood_front_x,10));
    }
    if(Rotate<-90&&Rotate>-270){
        //painter->drawPixmap(-pix[state].width() * 0.5,-pix[state].height()*0.5,pix[state]);
        painter->drawPixmap(-pix[state+3].width()*0.5,-pix[state+3].height() * 0.5,pix[state+3]);

    }else{
        //painter->drawPixmap(-pix[state+3].width()*0.5,-pix[state+3].height() * 0.5,pix[state+3]);
        painter->drawPixmap(-pix[state].width() * 0.5,-pix[state].height()*0.5,pix[state]);
    }
    if(stopTime%40==0){
        state = (state+1)%3;
    }
}

void BigEnemy::advance(int phase)
{
    stopTime = (stopTime+1)%21;
    if(stopTime%20==0){
        if(isVisible()){
            if(mapToScene(0,0).x()<-150||mapToScene(0,0).y()<-150||mapToScene(0,0).x()>1750||mapToScene(0,0).y()>1050){
                setVisible(false);
                isshowblood = 0;
                current_blood = blood;
                current_blood_front_x = blood_front_x;
            }else{
                setStartRotate(StartRotate);
                setPos(mapToScene(speed,0));
                setRotation(Rotate);
            }
        }
    }
}

void BigEnemy::setPos(const QPointF &pos)
{
    QGraphicsItem::setPos(pos);
}

void BigEnemy::setVisible(bool visible)
{
    QGraphicsItem::setVisible(visible);
}

void BigEnemy::setRotation(float angle)
{
    Rotate = angle;
    if(Rotate<-90&&Rotate>-270){
        QGraphicsItem::setRotation(180+angle);
    }else{
        QGraphicsItem::setRotation(angle);
    }
    //Enemy::setRotation(angle);
}

void BigEnemy::setBlood(float damage)
{
    current_blood -= damage;
    current_blood_front_x -= blood_front_x * (damage/blood);
    isshowblood = 1;
    update();
}

bool BigEnemy::isVisible()
{
    return QGraphicsItem::isVisible();
}

void BigEnemy::setStartRotate(float angle)
{
    StartRotate = angle;
    QGraphicsItem::setRotation(angle);
}
